package com.rui1024.dao.impl;

import com.rui1024.dao.CurdDao;
import com.rui1024.pojo.Curd;
import com.rui1024.util.PageUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class CurdDaoImpl implements CurdDao {

    //查询所有菜单数据
    public List<Curd> findAll(Connection con, PageUtil pageUtil) throws Exception{
        StringBuffer sb = new StringBuffer();
        sb.append("select * from menu");
        if(pageUtil!=null){
            sb.append(" limit "+pageUtil.getStart()+","+pageUtil.getPageSize()+"");
        }
        PreparedStatement pstmt = con.prepareStatement(sb.toString());

        List<Curd> list = new ArrayList<Curd>();
        ResultSet rs = pstmt.executeQuery();

        while (rs.next()) {
            Curd crud = new Curd();
            crud.setName(rs.getString("name"));
            crud.setChef(rs.getString("chef"));
            crud.setPrice(rs.getInt("price"));
            list.add(crud);
        }
        return list;
    }

    //删除菜单数据
    public boolean delete(String id, Connection con) throws Exception {
        String sql = "delete from menu where name=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, id);
        int num = pstmt.executeUpdate();
        if (num > 0) {
            return true;
        }
        return false;
    }

    //为菜单添加数据
    public boolean insert(Curd curd, Connection con) throws Exception {

        String sql="insert into menu(name,chef,price) values(?,?,?)";
        PreparedStatement pstmt = con.prepareStatement(sql);

        pstmt.setString(1, curd.getName());
        pstmt.setString(2, curd.getChef());
        pstmt.setInt(3, curd.getPrice());
//        pstmt.setInt(4, curd.getId());

        int num = pstmt.executeUpdate();
        if (num > 0) {
            return true;
        }
        return false;
    }

    // 根据id查找指定菜品
    public Curd find(String id,Connection con) throws Exception{

        String sql = "select * from menu where name=?";
        Curd curd = new Curd();
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, id);
        ResultSet rs = pstmt.executeQuery();
            // 处理结果集
            while (rs.next()) {
                curd.setName(rs.getString("name"));
                curd.setChef(rs.getString("chef"));
                curd.setPrice(rs.getInt("price"));
                return curd;
            }
            return null;
    }

    // 修改菜单
    public boolean update(Curd curd,Connection con) throws Exception{

        String sql = "update menu set price=?,chef=? where name=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
            //得到预编译对象

        pstmt.setString(3, curd.getName());
        pstmt.setString(2, curd.getChef());
        pstmt.setInt(1, curd.getPrice());

            int num = pstmt.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        }

    public int menuCount(Connection con) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COUNT(*) AS totalNum FROM menu ");
        PreparedStatement pstmt = con.prepareStatement(sb.toString());
        ResultSet rs = pstmt.executeQuery();
        if(rs.next()){
            return rs.getInt("totalNum");
        }else{
            return 0;
        }
    }

}

